﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using LoanCalculationService.Objects;
using System.Data.Common;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
using System.Data;
using BLLoanCalculation;
using BLLayer;

namespace LoanCalculationService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://au.Swindles.LoanCalculationService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LoanCalculationService : System.Web.Services.WebService
    {

		/// <summary>
		/// Follows the amortization formula to calculate the repayment value
		/// </summary>
		/// <param name="pLoanAmount"></param>
		/// <param name="pLoanTermm"></param>
		/// <param name="pBankIntrestRate"></param>
		/// <returns>monthlyRepaymentValue</returns>
		[WebMethod]
		public decimal getAnnualPremium(
			int pCreditRating, decimal pLoanAmount)
		{
			return BLLoanCalculation.BLLoanCalculation.CalculateAnnualInsurPremium(pCreditRating, pLoanAmount);
		}



		/// <summary>
        /// Checks the loan Eligibility based on business Rules
        /// The following business rules are checked
        /// 1) The amount of the loan requested should not exceed  x% of the current value of the property as estimated by the PropertyEvaluationService  
        /// --60% if the borrower has a credit rating of 3  --80% if the borrower has a credit rating of 4  --100% if the borrower has a credit rating of 5 The balance of the purchase price must be provided by the customer. 
        /// 2) Loan repayments will not exceed 50% of the applicants disposable income
        /// </summary>
        /// <param name="pLoanAmount"></param>
        /// <param name="pLoanTerm"></param>
        /// <param name="pPropertyValue"></param>
        /// <param name="pAnnualdisposableIncome"></param>
        /// <param name="pPropertyYearBuilt"></param>
        /// <returns></returns>
        [WebMethod]
        public bool checkLoanEligibility(
            decimal pLoanAmount,
            decimal pLoanTerm,
            decimal pPropertyValue,
            decimal pAnnualdisposableIncome,
            int pPropertyYearBuilt )
        {
			string errorMsg = string.Empty;
			return BLLoanCalculation.BLLoanCalculation.checkLoanEligibility(
				pLoanAmount,
				pLoanTerm,
				pPropertyValue,
				pAnnualdisposableIncome,
				pPropertyYearBuilt,
				out errorMsg );
        }

        /// <summary>
        /// Follows the amortization formula to calculate the repayment value
        /// </summary>
        /// <param name="pLoanAmount"></param>
        /// <param name="pLoanTermm"></param>
        /// <param name="pBankIntrestRate"></param>
        /// <returns>monthlyRepaymentValue</returns>
        [WebMethod]
		public decimal getMortgageRepayment( 
            decimal pLoanAmount,
            decimal pLoanTerm,
            decimal pBankIntrestRate,
            decimal pLoanInsurance)
        {
			string errorMsg = string.Empty;
			return BLLoanCalculation.BLLoanCalculation.CalculateMonthlyLoanRepayment(pLoanAmount, pLoanTerm, pBankIntrestRate, pLoanInsurance, out errorMsg);
        }

        /// <summary>
        /// Store the quote details against the customer
        /// </summary>
        /// <param name="pCustomerID"></param>
        /// <param name="pQuoteObject"></param>
        /// <returns>Quotenumber</returns>
        [WebMethod]
        public string updateLoanQuote(
		  int pQuoteID,
		  int pCustomerID,
		  string pPrptyType,
		  decimal pPrptySize,
		  int pPrptyYearBuilt,
		  string pPrptyAddressLine1,
		  string pPrptyAddressLine2,
		  string pPrptySuburb,
		  string pPrptyState,
		  int pPrptyPostcode,
		  decimal pPrptyValue,
		  decimal pLoanAmount,
		  int pLoanTerm,
		  decimal pLoanIntrest,
		  string pLoanInsurance,
		  decimal pLoanMRepayment,
		  DateTime pQuoteDate)
        {
			string resultQuoteNumber = string.Empty;
			BLQuote.updateQuote(pQuoteID,
			  pCustomerID,
			  pPrptyType,
			  pPrptySize,
			  pPrptyYearBuilt,
			  pPrptyAddressLine1,
			  pPrptyAddressLine2,
			  pPrptySuburb,
			  pPrptyState,
			  pPrptyPostcode,
			  pPrptyValue,
			  pLoanAmount,
			  pLoanTerm,
			  pLoanIntrest,
			  pLoanInsurance,
			  pLoanMRepayment,
			  pQuoteDate,
			  out resultQuoteNumber );
			return resultQuoteNumber;
        }

		/// <summary>
		/// Returns the quote object based on the quoteID
		/// </summary>
		/// <param name="pQuoteID"></param>
		/// <returns>Retrieves details based on PK</returns>
        [WebMethod]
		public Quote getLoanQuote(int pQuoteID)
        {
			return BLQuote.getQuoteByID(pQuoteID);
        }
    }
}
